package by.grsu.shedule.fragment;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import by.grsu.shedule.R;
import by.grsu.shedule.adapter.DayAdapter;
import by.grsu.shedule.database.ImportantLessonManager;
import by.grsu.shedule.database.SheduleManager;
import by.grsu.shedule.model.DayFromDB;

public class DayFragment extends Fragment {

	private static final String TAG = DayFragment.class.getSimpleName();

	private DayAdapter adapter;

	private ListView listView;

	private List<DayFromDB> dayList;

	private static String ARG_DAY = "current_day";

	private SheduleManager manager;

	private ImportantLessonManager importantLessonManager;

	private DayFragment() {

	}

	public static DayFragment newInstance(String date) {
		Bundle bundle = new Bundle();
		bundle.putString(ARG_DAY, date);
		DayFragment fragment = new DayFragment();
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		listView = (ListView) getView().findViewById(R.id.listView);
		dayList = manager.getDayLesson(getArguments().getString(ARG_DAY));
		adapter = new DayAdapter(getActivity(), R.layout.day, dayList);
		listView.setAdapter(adapter);
		// listView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1,
		// int position, long arg3) {
		// importantLessonManager.addNewLesson(dayList.get(position)
		// .getSubject(), dayList.get(position).getTeacher(),
		// dayList.get(position).getGroup());
		// Toast.makeText(
		// getActivity(),
		// dayList.get(position).getSubject()
		// + dayList.get(position).getTeacher()
		// + dayList.get(position).getGroup(),
		// Toast.LENGTH_SHORT).show();
		// adapter.notifyDataSetChanged();
		//
		// }
		// });
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (manager == null) {
			manager = (SheduleManager) getActivity().getApplicationContext()
					.getSystemService(SheduleManager.SHEDULE_MANAGER);

		}
		if (importantLessonManager == null) {
			importantLessonManager = (ImportantLessonManager) getActivity()
					.getApplicationContext().getSystemService(
							ImportantLessonManager.IMPORTANT_LESSON_MANAGER);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, container, false);
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume " + getArguments().getSerializable(ARG_DAY));
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.d(TAG, "onSaveInstanceState");
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause " + getArguments().getSerializable(ARG_DAY));
	}

	public void refreshData(String date) {
		getArguments().putString(ARG_DAY, date);
		if (adapter != null) {
			dayList = manager.getDayLesson(getArguments().getString(ARG_DAY));
			adapter.setList(dayList);
			adapter.notifyDataSetChanged();
		}
	}

	public void clearData() {
		if (adapter != null) {
			dayList.clear();
			adapter.setList(dayList);
			adapter.notifyDataSetChanged();
		}
	}

}
